//
//  UISegmentedControl_Custom.m
//  ASI
//
//  Created by Shi Zijie on 12-3-12.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UISegmentedControl_Custom.h"

@implementation UISegmentedControl_Custom

@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        UIImageView* backgroudImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        backgroudImageView.image = [UIImage imageNamed:@"bg_system_title.png"];
        [self addSubview:backgroudImageView];
        [backgroudImageView release];
        
        UIImage* image = [UIImage imageNamed:@"btn_tap1_normal.png"];
        
        firstButton = [UIButton buttonWithType:UIButtonTypeCustom];
        firstButton.frame = CGRectMake(8, 9, image.size.width, image.size.height);
        [firstButton setBackgroundImage:[UIImage imageNamed:@"btn_tap1_normal.png"] forState:UIControlStateNormal];
        [firstButton setBackgroundImage:[UIImage imageNamed:@"btn_tap1_press.png"] forState:UIControlStateSelected];
        [firstButton setTitle:@"我的旅途" forState:UIControlStateNormal];
        firstButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        firstButton.tag = 199;
        [firstButton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:firstButton];
        //[firstButton release];
        
        secondButton = [UIButton buttonWithType:UIButtonTypeCustom];
        secondButton.frame = CGRectMake(8+image.size.width, 9, image.size.width, image.size.height);
        [secondButton setBackgroundImage:[UIImage imageNamed:@"btn_tap2_normal.png"] forState:UIControlStateNormal];
        [secondButton setBackgroundImage:[UIImage imageNamed:@"btn_tap2_press.png"] forState:UIControlStateSelected];
        [secondButton setTitle:@"我的收藏" forState:UIControlStateNormal];
        secondButton.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        secondButton.tag = 299;
        [secondButton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:secondButton];
        //[secondButton release];
        
        [firstButton setSelected:YES];
        [secondButton setSelected:NO];
        selectIndex = 0;
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
- (void)dealloc {
    [firstButton release];firstButton = nil;
    [secondButton release];secondButton = nil;
    [super dealloc];
}

- (void)buttonPress:(id)sender
{
    UIButton* button = (UIButton*)sender;
    if (button.tag == 199 && selectIndex != 0) 
    {
        [firstButton setSelected:YES];
        [secondButton setSelected:NO];
        selectIndex = 0;
        [delegate UISegmentedControlEventValueChanged:selectIndex];
    }
    else if (button.tag == 299 && selectIndex != 1)
    {
        [firstButton setSelected:NO];
        [secondButton setSelected:YES];
        selectIndex = 1;
        [delegate UISegmentedControlEventValueChanged:selectIndex];
    }
    else
    {
        NSLog(@"nil");
    }
}

@end
